package com.mm.controller;

import com.mm.bo.AttendanceInsertBo;
import com.mm.bo.AttendanceSearchBo;
import com.mm.bo.AttendanceUpdateBo;
import com.mm.exception.CustomException;
import com.mm.service.AttendanceInfoService;
import com.mm.util.GetHeadUtil;
import com.mm.util.PageUtil;
import com.mm.util.ResultUtil;
import com.mm.vo.AttendanceInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @ClassName AttendanceInfoController
 * @Description TODO
 * @Author 任昊
 * @Date 2020/12/4 13:41
 */
@RestController
@Api(tags = "用于人事管理考勤的接口")
public class AttendanceInfoController {

    @Resource
    private AttendanceInfoService attendanceInfoService;

    /**
     * 查询员工考勤信息
     *
     * @param attendanceSearchBo
     * @return
     */
    @ApiOperation(value = "查询员工考勤", notes = "查询条件(时间（年/月）、工号、部门、姓名)")
    @PostMapping("/v1/attendance/selectAttendance")
    public ResultUtil<Object> selectAttendance(@RequestBody @Valid AttendanceSearchBo attendanceSearchBo) {
        PageUtil<AttendanceInfoVo> pageUtil = attendanceInfoService.selectAttendanceInfo(attendanceSearchBo);
        return ResultUtil.success(pageUtil);
    }

    /**
     * 修改员工考勤信息
     *
     * @param attendanceUpdateBo
     * @return
     */
    @ApiOperation(value = "修改员工考勤", notes = "修改员工考勤")
    @PostMapping("/v1/attendance/updateAttendance")
    public ResultUtil updateAttendance(@RequestBody @Valid AttendanceUpdateBo attendanceUpdateBo) {
        attendanceUpdateBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = attendanceInfoService.updateAttendanceInfo(attendanceUpdateBo);
        if (row < 1) {
            throw new CustomException("修改失败");
        }
        return ResultUtil.success(null);
    }

    /**
     * 新增员工考勤信息
     *
     * @param attendanceInsertBo
     * @return
     */
    @ApiOperation(value = "新增员工考勤", notes = "新增员工考勤")
    @PostMapping("/v1/attendance/insertAttendance")
    public ResultUtil insertAttendance(@RequestBody @Valid AttendanceInsertBo attendanceInsertBo) {
        attendanceInsertBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = attendanceInfoService.insertAttendanceInfo(attendanceInsertBo);
        if (row < 1) {
            throw new CustomException("新增失败");
        }
        return ResultUtil.success(null);
    }

}
